import 'package:flutter/material.dart';
import 'package:flutter_demo/ui/pages/composite/index.dart';
import 'package:flutter_demo/ui/pages/getx/count/count_controller.dart';
import 'package:get/get.dart';

// ignore: slash_for_doc_comments
/**
 * 处理页面展示
 */
class Count extends StatelessWidget {
  static const String routeName = "/getx/count";
  const Count({super.key});

  @override
  Widget build(BuildContext context) {
    final user = User().obs;
    user.value = User(age: 18, name: "liy");
    //user 中任意属性编号都会被监听到
    user.listen((p0) {
      print("----user.listen-----");
    });
    //final countController = Get.find<CountController>();
    print("-----");
    return Scaffold(
      appBar: AppBar(
        title: const Text("Getx count"),
        centerTitle: true,
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Container(
            height: 200,
            width: 200,
            color: Colors.red,
            child: Align(
              alignment: Alignment.center,
              child: Obx(
                () => Text(
                  //countController.get().toString(),
                  CountController.to.get().toString(),
                  style: const TextStyle(fontSize: 50, color: Colors.white),
                ),
              ),
            ),
          ),
          GetX<CountController>(
            builder: (controller) {
              print("build----- GetX<CountController>");
              return Text('${controller.get().toString()}');
            },
          ),
          Obx(
            () => Text(
              '${user.value.name}',
              style: const TextStyle(fontSize: 50, color: Colors.white),
            ),
          ),
          ElevatedButton(
              child: const Text("obs class  update"),
              onPressed: () {
                user.value.name = "CCTV";
                // user.update((val) {
                //   val?.name = "tc ++";
                // });
              }),
          GetBuilder<CountController>(
            builder: (controller) => Text('${controller.getSimpleCount()}'),
          ),
          GetBuilder<CountController>(
            id: "test1", //精细控制
            builder: (controller) => Text('${controller.getSimpleCount()}'),
          ),
          ElevatedButton(
              child: const Text("GetBuilder "),
              onPressed: () {
                //countController.addSimpleCount();
                CountController.to.addSimpleCount();
              }),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.add),
        onPressed: () {
          //countController.add();
          CountController.to.add();
          Get.toNamed(CompositePage.routeName);
        },
      ),
    );
  }
}

class User {
  User({this.age = 0, this.name = "tc"});
  String name;
  int age;
}
